package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.dto.NursingProjectPageDTO;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Override
    public PageResponse pageCondition(NursingProjectPageDTO nursingProjectPageDTO) {
        PageHelper.startPage(nursingProjectPageDTO.getPageNum(),nursingProjectPageDTO.getPageSize());
        Page<NursingProjectVo> page =  nursingProjectMapper.pageCondition(nursingProjectPageDTO);
        PageResponse<NursingProjectVo> pageResponse = PageResponse.of(page, NursingProjectVo.class);
        for (NursingProjectVo record : pageResponse.getRecords()) {
            record.setCount(nursingProjectMapper.projectRefCount(record.getId()));
        }
        return pageResponse;
    }

    @Override
    public void insert(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(nursingProjectDto,nursingProject);
        nursingProjectMapper.insert(nursingProject);
    }

    @Override
    public NursingProjectVo getById(Long id) {
      NursingProjectVo nursingProjectVo =  nursingProjectMapper.getById(id);
        return nursingProjectVo;
    }

    @Override
    public void update(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = NursingProject.builder().build();
        BeanUtils.copyProperties(nursingProjectDto,nursingProject);
        nursingProjectMapper.update(nursingProject);
    }

    @Override
    public List<NursingProjectVo> selectAll() {

        return  nursingProjectMapper.selectAll();
    }
}
